﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessGroup
// 文件功能描述：班组数据访问层
//
// 
// 创建标识：2012-02-14 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessGroup
    {
        /// <summary>
        /// 获取班组信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelGroupInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY ID ASC) rownumber,* " +
                               "from Con_Group) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString() + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取食堂信息
        /// </summary>
        /// <returns></returns>
        public DataSet GetWarehouse_ID()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select ID,Warehouse_Name from  Con_Warehouse";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取食堂信息1
        /// </summary>
        /// <returns></returns>
        public DataSet GetWarehouse_ID1()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select ID,Warehouse_Name from  Con_Warehouse where Flag=1";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 班组数量
        /// </summary>
        /// <returns></returns>
        public int GetGroupCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Group";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 保存班组(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveGroup(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "insert into Con_Group(Node_ID,Group_Name,Remark) Values('" +
                supp[0] + "','" + supp[1] + "','" + supp[2] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 保存班组(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveGroupEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update Con_Group set Node_ID=" + supp[1] + ",Group_Name='" + supp[2] + "',Remark='" + supp[3] + "' where ID=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteGroup(string id)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from Con_Instock_Detailed where Group_ID in(" + id + ")";
            int rowcount = Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            query = "select count(*) from Con_Outstock_Detailed where Group_Id in(" + id + ")";
            rowcount += Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            if (rowcount > 0)
            {
                rowcount = 0;
            }
            else
            {
                query = "Delete from Con_Group where ID in(" + id + ")";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条班组信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public DataSet GetGroupInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from Con_Group where ID = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        public static string GetGroupNameById(int id)
        {
            sqldb db = new sqldb();
            string query = "select isnull((select Group_Name from Con_Group where ID = " + id.ToString() + "),'') Group_Name";
            object gname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return gname.ToString();
        }

        public DataSet GetNodeID()
        {
            sqldb db = new sqldb();
            string query = "select NodeID,Nodename from Yax_Node";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }
    }
}
